if (!jsx3.lang.Class.forName("ux.cdb.admin.loan.LoanManage")) {
    /**
     * Define the <code>name of class</code> class.
     * This class inherit from <code>jsx3.gui.Block<code> GUI class.
     */
    jsx3.Class.defineClass("ux.cdb.admin.loan.LoanManage", jsx3.gui.Block, null,
    function(loanManage) {

        loanManage.prototype.homedir = 'ux/cdb/admin/loan/';

        loanManage.prototype.bind = function(fn, scope) {
            scope = scope || this;
            return function() {
                fn.apply(scope, arguments)
            }
        };

        /**
         * This method will be called after UI file has deserialized.
         */
        loanManage.prototype.onAfterDeserialize = function() {
            var me = this;

            this.init();
            var  privilegebtnNew = ux.cdb.admin.Privilege.getPrivilege(16);
						if(privilegebtnNew == null){
							this.get("btnAddUser").hide();
						}else{
							this.get("btnAddUser").enable();
						}
						var  privilegebtnEdit = ux.cdb.admin.Privilege.getPrivilege(17);
						if(privilegebtnEdit == null){
							this.get("btnEditUser").hide();
						}else{
							this.get("btnEditUser").enable();
						}
						var  privilegebtnVerify = ux.cdb.admin.Privilege.getPrivilege(18);
						if(privilegebtnVerify == null){
							this.get("btnVerify").hide();
						}else{
							this.get("btnVerify").enable();
						}
						var  privilegebtnSign = ux.cdb.admin.Privilege.getPrivilege(19);
						if(privilegebtnSign == null){
							this.get("btnSignContract").hide();
						}else{
							this.get("btnSignContract").enable();
						}
						var  privilegebtnImport = ux.cdb.admin.Privilege.getPrivilege(20);
						if(privilegebtnImport == null){
							this.get("btnImport").hide();
						}else{
							this.get("btnImport").enable();
						}
            
            this.blkField_applyDateSelect = this.get('blkField_applyDate').getChild(1);
            this.blkField_studentName = this.get('blkField_studentName').getChild(1);
            this.blkField_applyNumber = this.get('blkField_applyNumber').getChild(1);
            this.blkField_studentCertId = this.get('blkField_studentId').getChild(1);
            this.blkField_schoolName = this.get('blkField_schoolName').getChild(1);

            this.matrix = this.get("mtxMain");
            this.matrix.onSelect = this.bind(me._onMatrixSelect, me);
            this.matrix.subscribe(ux.cdb.admin.gui.Matrix.EVENT_CHECKALL, me, me._onMatrixCheckAll);
            var checkbox = this.matrix.get("checkbox");
            checkbox.onToggle = this.bind(me._onCheckMatrix, me);
            this.loadFilter();
            //this.loadApplyDate();
            //this.loadMatrixData(); //to be delete

            this.pageCtrler = this.get("blkPageWheel");

            this.defaultPageSize = 20;
            this.totalRecordCount = 100;
            this.currentPage = 1;
            this.pageControlInit = false;
            this.totalPages = 0;

            /*
            this.pageCtrler.initialize();
            this.pageCtrler.setValue(this.currentPage,toltopages,this.defaultPageSize,this.totalRecordCount);
            this.pagebar.bindWithMatrix(this, this.loadContractList, this.matrix);        
            */
            this.currentSearchMethod = 1; //1:getAll  2:getBy
						var tip = ux.megatron.Main.getServer().getDynamicProperty("@cdb@LoanManage@MainPage@PleaseSelect");
            this.get('selFilter_0602').setValue(tip);
            this.get('selFilter_0602').repaint();
            this.get('selFilter_0602').OnChange = this.bind(me._onFilter, me);
        		this.get("btnRefresh").OnRefresh = this.bind(me.onRefresh, me);
						this.UpdateButtons();
            //this.refrechAllData();
        }

        loanManage.prototype.onSearchCallBack = function(reData) {
        	jsx3.log("onSearchCallBack");
            if (!reData) jsx3.log("call Search failed......");
            //jsx3.log(ux.megatron.util.JSON.JSONtoString(reData));
            var flag = reData.res.flag;
            if (flag != 1) {
                var msg = reData.res.msg;
                jsx3.log("call Search failed: " + msg);
            }

            var reList = reData.content.data;
            jsx3.log("reList.length:"+reList.length);
						//jsx3.log("reList.virtualApproveObjectId:"+reList[0].virtualApproveObjectId);
            var cdfDoc = ux.megatron.util.Dwr.toCdfDocument(reList, "jsxroot", {
                "appBasicInfoId": "jsxid"
            });
            //jsx3.log("cdfDoc: " + cdfDoc);
            var records = cdfDoc.selectNodes("//record");
            for (var i = 0; i < records.size(); i++) {
                var record = records.get(i);
                var status = record.getAttribute("appStatusCode");
                var tipwaitingVerify = ux.megatron.Main.getServer().getDynamicProperty("@cdb@LoanManage@MainPage@waitingVerify");
                var tipwaitingSign = ux.megatron.Main.getServer().getDynamicProperty("@cdb@LoanManage@MainPage@waitingSign");
                var tipreturned = ux.megatron.Main.getServer().getDynamicProperty("@cdb@LoanManage@MainPage@returned");
                var tiprefuse = ux.megatron.Main.getServer().getDynamicProperty("@cdb@LoanManage@MainPage@refuse");
                if (status == "01") {
                    record.setAttribute("appStatusCode", tipwaitingVerify);
                } else if (status == "02") {
                    record.setAttribute("appStatusCode", tipwaitingSign);
                } else if (status == "13") {
                    record.setAttribute("appStatusCode", tipreturned);
                } else if (status == "14") {
                    record.setAttribute("appStatusCode", tiprefuse);
                }
                try{
                	var applyDate = record.getAttribute("appAcademicYear");
                	if(applyDate!=null && applyDate.trim().length==8 ){
                		var tip = ux.megatron.Main.getServer().getDynamicProperty("@cdb@LoanManage@MainPage@XueNian");
                		var year1 = applyDate.substring(0,4);
                		var year2 = applyDate.substring(4,8);
                		//jsx3.log("year2:"+year2);
                		applyDate = year1+"-"+year2+tip;
                		record.setAttribute("appAcademicYear", applyDate);
                	}
              	}catch(e){
					jsx3.log('applyDate format failed : ' + e.message);
				}
            }
            //this.matrix.clearXmlData();
            this.matrix.setSourceXML(cdfDoc);
            this.matrix.repaint();
						this.UpdateButtons();
            this.totalRecordCount = reData.content.page.count;
            this.totalPages = (this.totalRecordCount % this.defaultPageSize > 0) ? (parseInt(this.totalRecordCount / this.defaultPageSize) + 1) : (this.totalRecordCount / this.defaultPageSize);
            if (this.totalPages == 0) this.totalPages = 1;
            this.loadPageControler();
        }

        loanManage.prototype.loadPageControler = function() {
            if (!this.pageControlInit) {
                this.pageCtrler.initialize();
                this.pageControlInit = true;
            }
            //this.pageCtrler.setValue(this.currentPage,toltopages,this.defaultPageSize,this.totalRecordCount);
            this.pageCtrler.setValue(this.currentPage, this.totalPages, this.defaultPageSize, this.totalRecordCount);
            var me = this;
            this.pageCtrler.bindWithMatrix(me, me.onPageChange, me.matrix);
        }

        loanManage.prototype.refrechAllData = function() {
						this.init();
            var params = [];
            var status = this.get("selFilter_0602").getValue();
            params.push(status);
            params.push(this.currentPage);
            params.push(this.defaultPageSize);
            var me = this;
            ux.megatron.util.Dwr.callDwr("MyJobCountyUCC", "getMyWorkList", params, me, me.onSearchCallBack, "MyJobCountyUCC");
        }

        loanManage.prototype.onPageChange = function(msg) {
            this.currentPage = msg.currentPageNumber;
            var params = [];
            if (this.currentSearchMethod == 1) { //getAll
                this.refrechAllData();
            } else { //get by search condition
                this.onCustomSearch();
            }
        }

        loanManage.prototype.init = function() {
            this.get("tabedPane_block").hide();
            //this.get("blkSize").show();
            this.get("blkTipInfo").show();
			this.get('btnViewDetailInfo').getParent().hide();
        }

        loanManage.prototype.setBtns = function(enableBtns, disableBtns) {
            if (enableBtns) {
                for (var i = 0; i < enableBtns.length; i++) {
                    this.get(enableBtns[i]).enable();
                }
            }
            if (disableBtns) {
                for (var i = 0; i < disableBtns.length; i++) {
                    this.get(disableBtns[i]).disable();
                }
            }
        };

        loanManage.prototype.onClickSignContract = function() {
            var records = this.matrix.getCheckedNodes();
            if (records.size() > 1) {
                return;
            }
            var record = records.get(0);
            //var studentId = record.getAttribute("studentId");
            var strRECORDID = record.getAttribute("jsxid");
            var parent = ux.megatron.Main.getServer().getBodyBlock();
            //parent.applyRecordId = strRECORDID;
            ux.megatron.Main.loadResources("ux/cdb/admin/contract/xml/resources.xml", "ux.cdb.admin");
            ux.megatron.Main.loadUIResourceAsync(parent, "ux.cdb.admin", "ux/cdb/admin/contract/builder/ui/EditBuilderDialog.xml", this, this.onAddBuilderLoaded);
        }

        loanManage.prototype.onAddBuilderLoaded = function(objEvent) {
            var records = this.matrix.getCheckedNodes();
            if (records.size() > 1) {
                return;
            }
            var record = records.get(0);
            var studentId = record.getAttribute("studentId");
            var strRECORDID = record.getAttribute("jsxid");
            objEvent.obj.comeFrom = "LoanManage";
            objEvent.obj.applyId = strRECORDID;
            objEvent.obj.studentId = studentId;
        }

        loanManage.prototype.onClickImport = function() {
            var mainPane = this.getDescendantOfName('blkWorld');
            if (!mainPane) {
                mainPane = ux.megatron.Main.getServer().getBodyBlock();
            }
            var me = this;
            ux.megatron.Main.loadUIResourceAsync(mainPane, "ux.cdb.admin", "ux/cdb/admin/loan/ui/Importdialog2.xml", me, me.importdialogLoaded);
            //ux.megatron.Main.loadUIResourceAsync(mainPane,"ux.cdb.admin","ux/cdb/admin/loan/loanWizard/ui/loanBuilderDailog.xml",me,me.onAddWizardLoaded);
            //var parent = ux.megatron.Main.getServer().getBodyBlock();alert(11);
            //ux.megatron.Main.loadUIResourceAsync(parent,
            //        "ux.cdb.admin","ux/cdb/admin/loan/ui/Importdialog.xml", this,  this.importdialogLoaded);
        }

        loanManage.prototype.importdialogLoaded = function() {}

        loanManage.prototype.onCustomSearch = function() {
      			this.init();
            this.currentSearchMethod = 2;
            //this.get('selFilter_0602').setValue("02");
            var me = this;
            var studentName = this.blkField_studentName.getValue();
            var applyDate = this.blkField_applyDateSelect.getValue();
            var applyNumber = this.blkField_applyNumber.getValue();
            var studentCertId = this.blkField_studentCertId.getValue();
            var schoolName = this.blkField_schoolName.getValue();
            var appStatusCode = this.get('selFilter_0602').getValue();

						applyNumber = this.emptyToNull(applyNumber);
        		studentName = this.emptyToNull(studentName);
        		studentCertId = this.emptyToNull(studentCertId);
        		var ps = ux.megatron.Main.getServer().getDynamicProperty("@cdb@LoanManage@MainPage@PleaseSelect");
						if(appStatusCode == ps){
							return;
						}
												
            var params = [];
            params.push(applyNumber);
            params.push(studentName);
            params.push(studentCertId);
            params.push(this.currentPage);
            params.push(this.defaultPageSize);
            params.push(appStatusCode);
/* @JSC :: begin DEBUG */
            jsx3.log("onCustomSearch applyNumber:"+applyNumber);
            jsx3.log("onCustomSearch studentName:"+studentName);
            jsx3.log("onCustomSearch studentCertId:"+studentCertId);
            jsx3.log("onCustomSearch currentPage:"+this.currentPage);
            jsx3.log("onCustomSearch defaultPageSize:"+this.defaultPageSize);
            jsx3.log("onCustomSearch appStatusCode:" + appStatusCode);
/* @JSC :: end */
            var me = this;
            ux.megatron.util.Dwr.callDwr("RetailStudentLoanPreContractUCC", "getStudentAppListByCondition", params, me, me.onSearchCallBack, "RetailStudentLoanPreContractUCC");
        }

			loanManage.prototype.emptyToNull = function(sValue) {
      	var result = null;
      	if(sValue != null && sValue.trim().length > 0){
      		result = sValue;
      	}
      	return result;
      }

      loanManage.prototype.onRefresh = function() {
      	this.init();
      	if(this.currentSearchMethod == 1){   //getAll					
      		this.refrechAllData();
      	}else{//get by search condition
      		this.onCustomSearch();
      	}
      }
        loanManage.prototype.onCustomSearchCallBack = function(data) {
            //alert(data);
            }

        loanManage.prototype.UpdateButtons = function() {
            var nodes = this.matrix.getCheckedNodes();
            var filterStatus = this.get("selFilter_0602").getValue();
            if (nodes.size() == 0) {
                this.setBtns(['btnAddUser'], ['btnEditUser', 'btnDelete', 'btnVerify', 'btnSignContract']);
            } else if (nodes.size() == 1) {
                this.setBtns(['btnAddUser', 'btnEditUser', 'btnDelete', 'btnVerify', 'btnSignContract'], null);                
                if (filterStatus == "02") {
                    this.setBtns(['btnAddUser', 'btnDelete', 'btnSignContract'], ['btnVerify', 'btnEditUser']);
                } else if (filterStatus == "01") {
                    this.setBtns(['btnAddUser', 'btnDelete', 'btnVerify'], ['btnSignContract', 'btnEditUser']);
                } else if (filterStatus == "13") {
                    this.setBtns(['btnAddUser', 'btnEditUser', 'btnDelete'], ['btnSignContract', 'btnVerify']);
                } else {
                    this.setBtns(['btnAddUser', 'btnDelete'], ['btnSignContract', 'btnVerify', 'btnEditUser']);
                }
            } else {
            	if(filterStatus == "01"){
                this.setBtns(['btnAddUser', 'btnDelete', 'btnVerify'], ['btnEditUser', 'btnSignContract']);
              }else{
              	this.setBtns(['btnAddUser', 'btnDelete'], ['btnEditUser', 'btnSignContract', 'btnVerify']);
              }
            }
        }

        loanManage.prototype._onMatrixCheckAll = function() {
            this.UpdateButtons();
        }

        loanManage.prototype._onCheckMatrix = function() {
            this.UpdateButtons();
        }

        loanManage.prototype.onDetailLoaded = function() {
            //alert('OK!');
            }

        loanManage.prototype.oncoDetailLoaded = function() {
            this.CoDebitmatrix = this.get("tabDebitInfo").getContentPane().get("mtxCoDebit");
            //var me = this;
            this.loadCoDebitData();
            //this.get("btnRefresh").OnRefresh = this.bind(me.loadMatrixData, me);
        }

        loanManage.prototype.onTebbedPaneLoaded = function() {
            var tabLoanBlock = this.get('tabedPane_block');
			//tabLoanBlock.getFirstChild().loadTabDetail();
			tabLoanBlock.getFirstChild().initialize();
			this.get('btnViewDetailInfo').getParent().show();
        }

        loanManage.prototype.loadDetails = function(strRECORDID, studentID) {
            var tabLoanBlock = this.get('tabedPane_block');
            tabLoanBlock.selectRecordId = strRECORDID;
            tabLoanBlock.studentID = studentID;
            var me = this;
            if (!tabLoanBlock.getFirstChild()) {
                ux.megatron.Main.loadUIResourceAsync(tabLoanBlock, "ux.cdb.admin", "ux/cdb/admin/loan/ui/LoanManageTebbedpane.xml", me, me.onTebbedPaneLoaded);
            } else {
				tabLoanBlock.getFirstChild().selectRecordId = strRECORDID;
				tabLoanBlock.getFirstChild().studentId = studentID;
				this.onTebbedPaneLoaded();
            }
        }

        loanManage.prototype.loadTabDetail = function() {}

        loanManage.prototype._onMatrixSelect = function() {            
            var strRECORDID = this.matrix.getValue();
            var records = this.matrix.getSelectedNodes();
            if (records.size() != 1 ) {
                return;
            }
            
            this.get("tabedPane_block").show();
            //this.get("blkSize").hide();
            this.get("blkTipInfo").hide();
            
            var record = records.get(0);
            var studentId = record.getAttribute("studentId");
/* @JSC :: begin DEBUG */
            jsx3.log("LoanManage _onMatrixSelect: applyId" + strRECORDID);
            jsx3.log("LoanManage _onMatrixSelect: studentId" + studentId);      	
/* @JSC :: end */
            this.loadDetails(strRECORDID, studentId);
        }

        loanManage.prototype._onFilter = function(sID) {
            //if(this.currentSearchMethod == 1){
            this.UpdateButtons();
            this.currentSearchMethod = 1;
            this.refrechAllData();
            //}else{
            //    this.onCustomSearch();
            //}
        }

        loanManage.prototype._doAddUser = function() {
            var mainPane = this.getDescendantOfName('blkWorld');
            if (!mainPane) {
                mainPane = ux.megatron.Main.getServer().getBodyBlock();
            }
            mainPane.Operation = 'Insert';
            var me = this;
            ux.megatron.Main.loadUIResourceAsync(mainPane, "ux.cdb.admin", "ux/cdb/admin/loan/loanWizard/ui/loanBuilderDailog.xml", me, me.onAddWizardLoaded);
        }

        loanManage.prototype._doEditUser = function() {
            var records = this.matrix.getCheckedNodes();
            if (records.size() > 1) {
                return;
            }
            var record = records.get(0);
            var studentId = record.getAttribute("studentId");
            var strRECORDID = record.getAttribute("jsxid");
            //var mainPane = this.getDescendantOfName('blkWorld');

            var mainPane = ux.megatron.Main.getServer().getBodyBlock();
            mainPane.Operation = 'Edit';
            mainPane.studentId = studentId;
            mainPane.selectRecordId = strRECORDID;

            var me = this;
            ux.megatron.Main.loadUIResourceAsync(mainPane, "ux.cdb.admin", "ux/cdb/admin/loan/loanWizard/ui/loanBuilderDailog.xml", me, me.onEditWizardLoaded);
        }

        loanManage.prototype.onAddWizardLoaded = function(objEvent) {
            objEvent.obj.Operation = 'Insert';
            var strTitle = this.property("@cdb@LoanManage@Wizard@MainWizardAddTitle");
            objEvent.obj.get('labTitle').setText(strTitle, true);
        }

        loanManage.prototype.onEditWizardLoaded = function(objEvent) {
            objEvent.obj.comeFrom = "LoanManage";
            objEvent.obj.Operation = "Edit";
            var records = this.matrix.getCheckedNodes();
            var record = records.get(0);
            var studentId = record.getAttribute("studentId");
            var studentName = record.getAttribute("custName");
            var studentCertId = record.getAttribute("certId");
            var strRECORDID = record.getAttribute("jsxid");

            objEvent.obj.applyId = strRECORDID;
            objEvent.obj.studentName = studentName;
            objEvent.obj.studentCertId = studentCertId;
            objEvent.obj.studentId = studentId;

            var strTitle = this.property("@cdb@LoanManage@Wizard@MainWizardEditTitle");
            objEvent.obj.get('labTitle').setText(strTitle, true);
        }

        loanManage.prototype.property = function(name, token) {
            return ux.megatron.Main.getServer().getDynamicProperty(name, token);
        };

        loanManage.prototype._doDeleteUser = function() {
            var me = this;
            var tipInfo = this.property("@cdb@LoanManage@Wizard@DeleteLoanTipInfo");
            var tipTitle = this.property("@cdb@LoanManage@Wizard@DeleteLoanTipTitle");;
            var result = ux.megatron.Dialog.confirm(tipTitle, tipInfo,
            function(dialog) {
                var nodes = me.matrix.getCheckedNodes();
                for (var i = 0; i < nodes.size(); i++) {
                    me.matrix.deleteRecord(nodes.getItem(i).getAttribute('jsxid'));
                }
                me.matrix.repaintData();
                me.UpdateButtons();
                me.init();
                dialog.doClose();
            });
        }

        loanManage.prototype._doVerifyBorrow = function() {
            var mainPane = this.getDescendantOfName('blkWorld');
            if (!mainPane) {
                mainPane = ux.megatron.Main.getServer().getBodyBlock();
            }
            var me = this;
            ux.megatron.Main.loadUIResourceAsync(mainPane, "ux.cdb.admin", "ux/cdb/admin/loan/VerifyBorrow/ui/VerifyBorrow.xml", me, me.onVerifyLoaded);
        }

        loanManage.prototype.onVerifyLoaded = function(objEvent) {
            var nodes = this.matrix.getCheckedNodes();
            var appBaseInfos = [];
            for (var i = 0; i < nodes.size(); i++) {
                var record = nodes.getItem(i);
                var virtualId = record.getAttribute("virtualApproveObjectId");
                var appId = record.getAttribute("jsxid");
                var appBaseInfo = {appBasicInfoId:appId,virtualApproveObjectId:virtualId};
/* @JSC :: begin DEBUG */
                jsx3.log('virtualId:' + virtualId);
                jsx3.log('appId:' + appId);
/* @JSC :: end */
                appBaseInfos.push(appBaseInfo);
            }
            //jsx3.log('onVerifyLoaded virtualIds:' + virtualIds);
            objEvent.obj.appBaseInfos = appBaseInfos;
        }

        loanManage.prototype.loadFilter = function() {
            ux.megatron.Main.loadResource(this.homedir + "xml/SelFilter.xml", "ux.cdb.admin", "loanmanage_0602", "xml");
            this.get('selFilter_0602').repaint();
        }

        loanManage.prototype.loadApplyDate = function() {
            ux.megatron.Main.loadResource(this.homedir + "xml/applyDate.xml", "ux.cdb.admin", "loanmanage_0604", "xml");
            this.get('blkField_applyDate').getChild(1).repaint();
        }

        loanManage.prototype.loadMatrixData = function() {
            ux.megatron.Main.loadResource(this.homedir + "xml/LoanManage.xml", "ux.cdb.admin", "loanmanage_0601", "xml");
            this.matrix.repaint();
            this.init();
            this.UpdateButtons();
        }

        loanManage.prototype.loadCoDebitData = function() {
            ux.megatron.Main.loadResource(this.homedir + "xml/CoDebit.xml", "ux.cdb.admin", "loanmanage_0603", "xml");
            this.CoDebitmatrix.repaint();
        }
    });
}